var siteinfo = require("../siteinfo.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mch_id: null,
    to_user_id: null,
    to_user_nickname: null,
    to_user_img: null,
    url: siteinfo.kefu_host + '/#/message?is_wxapp=1&',
    list_url: siteinfo.kefu_host + '/#/list?is_wxapp=1&',
    goods_name: '',
    goods_img: '',
    goods_price: '',
    goods_type: 'STORE',
    goods_id: 0,
    iframe_url: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().page.onLoad(this, options);
    var user_info = getApp().core.getStorageSync("USER_INFO");
    if (!user_info) {
      getApp().page.setUserInfoShowUser()
      return;
    }
    var that = this;
    this.setData(options,function  () {
      var iframe_url = that.iframeUrl()
      that.setData({
        iframe_url: iframe_url
      })
    })
    wx.getSystemInfo({
      success (res) {
        wx.setStorageSync('headheight', res.statusBarHeight)
        that.setData({
            headheight: res.statusBarHeight
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  iframeUrl: function () {
    var store = wx.getStorageSync('STORE');
    var that = this;
    let store_id = store.id
    let host = siteinfo.apiroot.replace(/http(s)?:\/\//g, '')
    host = host.replace('/web/index.php?store_id=1&r=api/', '')
    if (this.data.mch_id != null) {
        let user_id =  host + '_' + this.data.mch_id + '_kefu_' + store_id
        return this.data.list_url + 'user_id=' + user_id
    }
    var user = wx.getStorageSync('USER_INFO');
    var userId = user && user.id > 0 ? user.id : 0;
    let user_id = host + '_' + userId + '_user_' + store_id
    if (this.data.is_user_list) {
      return this.data.list_url + 'user_id=' + user_id
    }
    let user_img = user && user.avatar_url ? user.avatar_url : '';
    let user_nickname = user && user.nickname ? user.nickname : '';
    if (this.data.goods_id>0) {
      return this.data.url + 'user_id=' + 
      user_id + '&to_user_id=' + 
      this.data.to_user_id + '&to_user_nickname=' + 
      this.data.to_user_nickname + '&to_user_img=' +
      this.data.to_user_img + '&user_img=' + 
      user_img + '&user_nickname=' +
      user_nickname + '&goods_name=' + this.data.goods_name + '&goods_id=' + this.data.goods_id + 
      '&goods_price=' + this.data.goods_price +
      '&goods_img=' + this.data.goods_img + 
      '&goods_type=' + this.data.goods_type
    }else {
      return this.data.url + 'user_id=' + 
      user_id + '&to_user_id=' + 
      this.data.to_user_id + '&to_user_nickname=' + 
      this.data.to_user_nickname + '&to_user_img=' +
      this.data.to_user_img + '&user_img=' + 
      user_img + '&user_nickname=' +
      user_nickname
    }

    
  },
})